using SqlSugar;using TradeFlow.Server.Language;using TradeFlow.Server.Models;

namespace TradeFlow.Server.Service
{
    /// <summary>
    /// 通用服务类，实现JxcLite中Repository的核心功能
    /// </summary>
    public class JxCommonService : IApplicationService
    {
        private readonly ISqlSugarClient _db;
        private readonly ICurrentUser _currentUser;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="db"></param>
        /// <param name="currentUser"></param>
        public JxCommonService(ISqlSugarClient db, ICurrentUser currentUser)
        {
            _db = db;
            _currentUser = currentUser;
        }

        /// <summary>
        /// 检查商品编码是否重复
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> ExistsGoodsCodeAsync(JxGoods model)
        {
            return await _db.Queryable<JxGoods>()
                .Where(u => u.Creator == _currentUser.UserId && u.Id != model.Id && u.Code == model.Code)
                .AnyAsync();
        }

        /// <summary>
        /// 检查商业伙伴名称是否重复
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task<bool> ExistsPartnerNameAsync(JxPartner model)
        {
            return await _db.Queryable<JxPartner>()
                .Where(u => u.Creator == _currentUser.UserId && u.Id != model.Id && u.Name == model.Name)
                .AnyAsync();
        }

        /// <summary>
        /// 获取最大业务单号
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public async Task<string> GetMaxBillNoAsync(string prefix)
        {
            var maxNo = await _db.Queryable<JxBillHead>()
                .Where(u => u.Creator == _currentUser.UserId && u.BillNo.StartsWith(prefix))
                .MaxAsync(u => u.BillNo);
            return maxNo;
        }

        /// <summary>
        /// 获取最大对账单号
        /// </summary>
        /// <param name="prefix"></param>
        /// <returns></returns>
        public async Task<string> GetMaxAccountNoAsync(string prefix)
        {
            var maxNo = await _db.Queryable<JxAccountHead>()
                .Where(u => u.Creator == _currentUser.UserId && u.AccountNo.StartsWith(prefix))
                .MaxAsync(u => u.AccountNo);
            return maxNo;
        }

        /// <summary>
        /// 生成新的单号（在原单号基础上加1）
        /// </summary>
        /// <param name="prefix"></param>
        /// <param name="maxNo"></param>
        /// <returns></returns>
        public string GenerateNewNo(string prefix, string maxNo)
        {
            if (string.IsNullOrEmpty(maxNo))
                return $"{prefix}001";

            // 提取数字部分并加1
            int num = 0;
            string numStr = maxNo.Substring(prefix.Length);
            if (int.TryParse(numStr, out num))
                return $"{prefix}{(num + 1):000}";
            
            return $"{prefix}001";
        }

        /// <summary>
        /// 根据日期生成前缀
        /// </summary>
        /// <param name="type"></param>
        /// <returns></returns>
        public string GeneratePrefix(string type)
        {
            string typePrefix = string.Empty;
            switch (type)
            {
                case BillType.Import:
                    typePrefix = "PO";
                    break;
                case BillType.ImportReturn:
                    typePrefix = "PR";
                    break;
                case BillType.Export:
                    typePrefix = "SO";
                    break;
                case BillType.ExportReturn:
                    typePrefix = "SR";
                    break;
                case "客户":
                    typePrefix = "CA";
                    break;
                case "供应商":
                    typePrefix = "SA";
                    break;
            }
            return $"{typePrefix}{DateTime.Now:yyyyMMdd}";
        }
    }
}